<template>
  <a-row :gutter="10">
    <a-col :md="leftColMd" :sm="24" style="margin-bottom: 20px">
      <a-card :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <!-- 搜索区域 -->
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <a-col :md="8" :sm="8">
                <a-form-item label="网站名称" :labelCol="{span: 5}" :wrapperCol="{span: 18, offset: 1}">
                  <j-input placeholder="" v-model="queryParam.name"></j-input>
                </a-form-item>
              </a-col>
              <a-col :md="11" :sm="12">
                <a-form-item label="创建时间" :labelCol="{span: 5}" :wrapperCol="{span: 18, offset: 1}">
                  <j-date v-model="queryParam.createTime_begin" :showTime="true" date-format="YYYY-MM-DD HH:mm:ss"
                          style="width:45%" placeholder="请选择开始时间"></j-date>
                  <span style="width: 10px;">~</span>
                  <j-date v-model="queryParam.createTime_end" :showTime="true" date-format="YYYY-MM-DD HH:mm:ss"
                          style="width:45%" placeholder="请选择结束时间"></j-date>
                </a-form-item>
              </a-col>
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="12" :sm="24">
               <a-button type="primary" @click="searchQuery" icon="search" style="margin-left: 21px">查询</a-button>
              <a-button @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </a-col>
          </span>
            </a-row>
          </a-form>
        </div>
        <!-- 操作按钮区域 -->
        <div class="table-operator" style="margin: 5px 0 10px 2px">
          <a-button @click="handleAdd" type="primary" icon="plus">新建网站</a-button>
          <a-dropdown v-if="selectedRowKeys.length > 0">
            <a-menu slot="overlay" @click="handleMenuClick">
              <a-menu-item key="1">
                <a-icon type="delete" @click="batchDel"/>
                删除
              </a-menu-item>
            </a-menu>
            <a-button style="margin-left: 8px">
              批量操作
              <a-icon type="down"/>
            </a-button>
          </a-dropdown>
        </div>

        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
          <i class="anticon anticon-info-circle ant-alert-icon">
          </i> 已选择 <a><b>{{ selectedRowKeys1.length }}</b></a>项
          <a style="margin-left: 24px" @click="onClearSelected1">清空</a>
        </div>

        <div style="margin-top: 15px">
          <a-table
            style="height:500px"
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            :rowSelection="{selectedRowKeys: selectedRowKeys1, onChange: onSelectChange1, type:'radio'}"
            @change="handleTableChange">
          <span slot="action" slot-scope="text, record">
             <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical"/>
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete1(record.id)">
                    <a>删除</a>
                  </a-popconfirm>
        </span>

        <span slot="CrawlTitle" slot-scope="text,record">
          <span  @click="handleOpen(record)"><a style="color: #1890FF">{{record.name}}</a></span>
        </span>
          </a-table>
        </div>
        <!-- 表单区域 -->
        <crawl-modal ref="modalForm" @ok="modalFormOk"></crawl-modal>
        <crawl-site-feature-doc-modal ref="modalForm2" @ok="modalFormOk2"></crawl-site-feature-doc-modal>
        <crawl-feature-doc-lists ref="CrawlFeatureDocLists"></crawl-feature-doc-lists>
      </a-card>
    </a-col>


    <!-- 右侧的爬虫文章配置 -->
    <a-col :md="rightColMd"  :sm="24"  v-if="this.rightcolval == 1">
      <a-card :bordered="false">
        <div style="text-align: right;">
          <a-icon type="close-circle" @click="hideUserList"/>
        </div>
        <div class="table-page-search-wrapper">
        <!-- 搜索区域 -->
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="12" :sm="12">
              <a-form-item label="文章名称" :labelCol="{span: 5}" :wrapperCol="{span: 18, offset: 1}">
                <a-input placeholder="" v-model="queryParam2.name"></a-input>
              </a-form-item>
            </a-col>
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="9" :sm="24">
               <a-button type="primary" @click="searchQuery2" icon="search" style="margin-left: 21px">查询</a-button>
              <a-button @click="searchReset2" icon="reload" style="margin-left: 8px">重置</a-button>
            </a-col>
          </span>
          </a-row>
        </a-form>
        </div>
        <!-- 操作按钮区域 -->
        <div class="table-operator" :md="24" :sm="24">
          <a-button @click="handleAdd2" type="primary" icon="plus">添加</a-button>
          <a-dropdown v-if="selectedRowKeys2.length > 0">
            <a-menu slot="overlay" @click="ArticleDelete">
              <a-menu-item key="1">
                <a-icon type="delete" @click="batchDel"/>
                删除
              </a-menu-item>
            </a-menu>
            <a-button style="margin-left: 8px">
              批量操作
              <a-icon type="down"/>
            </a-button>
          </a-dropdown>
        </div>
        <!-- table区域-begin -->
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
          <i class="anticon anticon-info-circle ant-alert-icon">
          </i> 已选择 <a><b>{{ selectedRowKeys2.length }}</b></a>项
          <a style="margin-left: 24px" @click="onClearSelected2">清空</a>
        </div>
        <div style="margin-top: 15px">
          <a-table
            style="height:500px"
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            :columns="columns2"
            :dataSource="dataSource2"
            :pagination="ipagination2"
            :loading="loading2"
            :rowSelection="{selectedRowKeys: selectedRowKeys2, onChange: onSelectChange2}"
            @change="handleTableChange2">
          <span slot="action" slot-scope="text, record">
              <a @click="CrawlFeature(record)"><a-icon type="setting"/>配置</a>
            <a-divider type="vertical"/>

            <a-dropdown>
              <a class="ant-dropdown-link">
                更多 <a-icon type="down"/>
              </a>
              <a-menu slot="overlay">
                 <a-menu-item>
                 <a @click="handleEdit2(record)">编辑</a>
                </a-menu-item>
                <a-menu-item>
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete2(record.id)">
                    <a>删除</a>
                  </a-popconfirm>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
        </span>

            <span slot="CssTitle" slot-scope="text,record">
          <el-tooltip class="item" effect="dark" :content="record.contentPatterns" placement="top">
            <span class="text-overflow">{{record.contentPatterns}}</span></el-tooltip>
        </span>
          </a-table>
        </div>
      </a-card>
    </a-col>

  </a-row>
</template>

<script>
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import moment from 'moment'
  import JInput from '@/components/jeecg/JInput'
  import CrawlModal from './modules/CrawlModal'
  import CrawlSiteFeatureDocModal from './modules/CrawlSiteFeatureDocModal'
  import CrawlFeatureDocLists from './CrawlFeatureDocLists'
  import {getAction, deleteAction} from '@/api/manage'
  import { filterObj } from '@/utils/util'

  export default {
    name: "CrawlList",
    mixins: [JeecgListMixin],
    components: {
      JInput, CrawlModal, CrawlSiteFeatureDocModal,CrawlFeatureDocLists,
      moment
    },
    data() {
      return {
        filters2: {},
        loading2: false,
        selectedRowKeys1: [],
        selectedRowKeys2: [],
        selectionRows1: [],
        selectionRows2: [],
        CrawlId: '',
        queryParam2: {},
        dataSource1: [],
        dataSource2: [],
        // 高级查询参数
        superQueryParams2: '',
        isorter1: {
          column: 'createTime',
          order: 'desc'
        },
        isorter2: {
          column: 'createTime',
          order: 'desc'
        },
        rightcolval:0,
        ipagination2: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        columns:
          [
            {
              title: '网站名称',
              align: 'center',
              dataIndex: 'name',
              scopedSlots: {customRender: 'CrawlTitle'}
            },
            {
              title: '网站地址',
              align: 'center',
              dataIndex: 'url'
            },
            {
              title: '创建时间',
              align: "center",
              dataIndex: 'createTime'
            },
            {
              title: '操作',
              dataIndex: 'action',
              align: 'center',
              scopedSlots: {customRender: 'action'}
            }
          ],

        columns2:
          [
            {
              title: '文章名称',
              align: 'center',
              dataIndex: 'name'
            },
            {
              title: '网站名称',
              align: 'center',
              dataIndex: 'websiteName'
            },
            {
              title: '样式',
              align: "center",
              width: 350,
              scopedSlots: {customRender: 'CssTitle'},
              dataIndex: 'contentPatterns'
            },
            {
              title: '操作',
              dataIndex: 'action',
              align: 'center',
              scopedSlots: {customRender: 'action'}
            }
          ],
        url: {
          list: "/sys/CrawlWebsite/list",
          eleteBatch: "/sys/CrawlWebsite/deleteBatch",
          delete: '/sys/CrawlWebsite/delete',
          deleteBatch: "/sys/CrawlWebsite/deleteBatch",

          list2: "/sys/CrawlSiteFeatureDocCont/list",
          eleteBatch2: "/sys/CrawlSiteFeatureDocCont/deleteBatch",
          delete2: '/sys/CrawlSiteFeatureDocCont/delete',
          ArticleDeleteBatch: "/sys/CrawlSiteFeatureDocCont/deleteBatch",
        },
      }
    },
    computed: {
      leftColMd() {
        return this.selectedRowKeys1.length === 0 ? 24 : 12
      },
      rightColMd() {
        return this.selectedRowKeys1.length === 0 ? 0 : 12
      }
    },
    methods: {

      modalFormOk2() {
        // 新增/修改 成功时，重载列表
        this.loadData2()
      },

      onSelectChange2(selectedRowKeys, selectionRows) {
        this.selectedRowKeys2 = selectedRowKeys
        this.selectionRows2 = selectionRows
      },

      handleOpen(record) {
        this.rightcolval = 1
        this.selectedRowKeys1 = [record.id]
        this.model1 = Object.assign({}, record)
        this.CrawlId = record.id
        this.onClearSelected2()
        this.loadData2()
      },

      onClearSelected2() {
        this.selectedRowKeys2 = []
        this.selectionRows2 = []
      },

      handleAdd2() {
        if (this.CrawlId == '') {
          this.$message.error('请选择一个网站!')
        } else {
          this.$refs.modalForm2.add(this.CrawlId);
        }
      },

      onSelectChange1(selectedRowKeys, selectionRows) {
        this.rightcolval = 1
        this.selectedRowKeys1 = selectedRowKeys
        this.selectionRows1 = selectionRows
        this.CrawlId = selectedRowKeys[0]
        console.log("========id========"+this.CrawlId)
        this.loadData2()
      },

      handleTableChange2(pagination, filters, sorter) {
        //分页、排序、筛选变化时触发
        //TODO 筛选
        if (Object.keys(sorter).length > 0) {
          this.isorter2.column = sorter.field
          this.isorter2.order = 'ascend' == sorter.order ? 'asc' : 'desc'
        }
        this.ipagination2 = pagination
        this.loadData2()
      },

      handleEdit: function (record) {
        this.$refs.modalForm.edit(record)
        this.$refs.modalForm.title = '编辑'
      },
      handleEdit2: function (record) {
        this.$refs.modalForm2.edit(record)
        this.$refs.modalForm2.title = '编辑'
      },

      handleDelete1: function (id) {
        this.handleDelete(id)
      },

      handleDelete2: function(id) {
        var that = this
        deleteAction(that.url.delete2,{id: id}).then((res) => {
          if (res.success) {
            that.$message.success(res.message)
            that.loadData2()
          } else {
            that.$message.warning(res.message)
          }
        })
      },

      hideUserList(){
        this.selectedRowKeys1 = []
      },

      searchQuery2() {
        this.loadData2(1)
      },
      searchReset2() {
        this.queryParam2 = {}
        this.loadData2(1)
      },

      loadData2(arg) {
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination2.current = 1
        }
        if (this.CrawlId === '') return
        let params = this.getQueryParams2()//查询条件
        params.websiteId = this.CrawlId
        this.loading2 = true
        getAction(this.url.list2, params).then((res) => {
          if (res.success) {
            this.dataSource2 = res.result.records
            this.ipagination2.total = res.result.total

          }
          this.loading2 = false
        })
      },

      ArticleDelete : function() {
        if (this.selectedRowKeys2.length <= 0) {
          this.$message.warning('请选择一条记录！');
          return;
        } else {
          var ids = "";
          for (var a = 0; a < this.selectedRowKeys2.length; a++) {
            ids += this.selectedRowKeys2[a] + ",";
          }
          var that = this;
          this.$confirm({
            title: "确认删除",
            content: "是否删除选中数据?",
            onOk: function () {
              deleteAction(that.url.ArticleDeleteBatch, {ids: ids}).then((res) => {
                if (res.success) {
                  that.$message.success(res.message);
                  that.loadData2()
                  that.onClearSelected()
                } else {
                  that.$message.warning(res.message);
                }
              });
            }
          });
        }
      },

      getQueryParams2() {
        //获取查询条件
        let sqp = {}
        if (this.superQueryParams2) {
          sqp['superQueryParams'] = encodeURI(this.superQueryParams2)
          sqp['superQueryMatchType'] = this.superQueryMatchType2
        }
        var param = Object.assign(sqp, this.queryParam2, this.isorter2, this.filters2)
        param.field = this.getQueryField2()
        param.pageNo = this.ipagination2.current
        param.pageSize = this.ipagination2.pageSize
        return filterObj(param)
      },
      onClearSelected1() {
        this.selectedRowKeys1 = []
        this.selectionRows1 = []
      },
      getQueryField2() {
        //TODO 字段权限控制
        var str = 'id,'
        this.columns2.forEach(function(value) {
          str += ',' + value.dataIndex
        })
        return str
      },

      CrawlFeature(record) {
        console.log("========record========" + JSON.stringify(record))
        this.$refs.CrawlFeatureDocLists.loadData2(record);
      },

      handleMenuClick(e) {
        if (e.key == 1) {
          this.batchDel();
        }
      },
    }

  }
</script>

<style scoped>
  /deep/.ant-card-body {
    padding: 2px;
    zoom: 1;
  }

  .text-overflow {
    display: inline-block;
    width: 350px !important; /* 设置元素的宽度 */
    white-space: nowrap; /* 保持文本在一行显示 */
    overflow: hidden; /* 隐藏超出部分 */
    text-overflow: ellipsis; /* 显示省略号 */
  }

</style>